import * as dayjs from 'dayjs'

// 格式化歌曲总时长
// example：timestamp = 238733, format = 'mm:ss' 转为 03:58 
export function formatTimestamp (timestamp, format = 'YYYY-MM-DD') {
  return dayjs.unix(timestamp / 1000).format(format);
}

// 格式化当前歌曲时长
export function formatDuration (duration) {
  const m = parseInt((duration / 60 % 60).toString())
  const mm = m.toString().padStart(2, '0')
  const s = parseInt((duration % 60).toString())
  const ss = s.toString().padStart(2, '0')
  return `${mm}:${ss}`
}

// 格式化的歌曲时长转换为秒钟
export function transformDuration (duration) {
  let arr = duration.split(':')
  let totalDuration = parseFloat(arr[0]) * 60 + parseFloat(arr[1]) * 1
  return totalDuration || 0
}